package com.example.sherlockzhong.plush_v2.Subscriptions;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.example.sherlockzhong.plush_v2.Components.buttons.BackButton;
import com.example.sherlockzhong.plush_v2.MapUtils.Coordinate;
import com.example.sherlockzhong.plush_v2.MapUtils.LocationsUtil;
import com.example.sherlockzhong.plush_v2.R;
import com.umeng.analytics.MobclickAgent;

public class StreetViewActivity extends Activity {

    public LocationClient mLocationClient = null;
    public BDLocationListener myListener = new MyLocationListener();

    private ImageView streetViewImage;
    private TextView streetViewText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_street_view);

        streetViewImage = (ImageView) findViewById(R.id.street_view_img);
        streetViewText = (TextView) findViewById(R.id.street_view_info);

        mLocationClient = new LocationClient(getApplicationContext());     //声明LocationClient类
        mLocationClient.registerLocationListener(myListener);

        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//设置定位模式
        option.setCoorType("bd09ll");
        option.setScanSpan(5000);
        option.setOpenGps(true);
        mLocationClient.setLocOption(option);

        mLocationClient.start();

        ((BackButton) findViewById(R.id.map_back)).setApp(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLocationClient.stop();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mLocationClient.stop();
        MobclickAgent.onPause(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public class MyLocationListener implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            if (location == null)
                return;
            Coordinate nearest = LocationsUtil.getNearest(location.getLongitude(), location.getLatitude());
            if (nearest != null) {
                streetViewText.setText("你现在在  " + nearest.name + "  附近");
                streetViewImage.setImageBitmap(BitmapFactory.decodeResource(getResources(), nearest.imageId));
            } else {
                streetViewText.setText("你已经离开了学校");
                streetViewImage.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.map_unknown));
            }
        }
    }
}
